/*
 * Created by SharpDevelop.
 * User: MyWife
 * Date: 1/1/2003
 * Time: 11:40 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Resources;
using System.Collections;
using popLib;
using System.Data;
using System.IO;

namespace ClockGadget
{
	/// <summary>
	/// Description of Options.
	/// </summary>
	public partial class Options : SidebarGadgetOption
	{
		DataTable table;
		
		
		public Options(SidebarGadget sidebarGadget):base(sidebarGadget)
		{
			InitializeComponent();
							
			for( KnownColor col = KnownColor.ActiveBorder; col < KnownColor.YellowGreen ; col++ ){
				cmb_hour.Items.Add(col.ToString());
				cmb_minute.Items.Add(col.ToString());
				cmb_second.Items.Add(col.ToString());
			}
			string TableData = "";
			string[] clocks = sidebarGadget.ini_config.getKeyNames( "Clocks" );
			foreach( string clock in clocks ){
				TableData += clock + "," + sidebarGadget.ini_config.getValue( "Clocks", clock )+"&";
			}
			
			table = DataLib.parseTextToTable( TableData, "Clock [ #Name, Hour, Minute, Second, TransparentBG ]", true );
			lbx_list.DataSource = table;
			lbx_list.DisplayMember = "Name";
			lbx_list.ValueMember   = "Name";
			
			lbx_list.Text = sidebarGadget.Options[Start.optionsType.Name+""]+"";
			cmb_hour.Text = sidebarGadget.Options[Start.optionsType.Hour+""]+"";
			cmb_minute.Text = sidebarGadget.Options[Start.optionsType.Minute+""]+"";
			cmb_second.Text = sidebarGadget.Options[Start.optionsType.Second+""]+"";
		}
		
		void ActionEvent(object sender, EventArgs e)
		{
			switch( ((Control)sender).Name ){
				case "lbx_list": //index changed
					try{
						cmb_hour.Text   = table.Rows[lbx_list.SelectedIndex]["Hour"]+"";
						cmb_minute.Text = table.Rows[lbx_list.SelectedIndex]["Minute"]+"";
						cmb_second.Text = table.Rows[lbx_list.SelectedIndex]["Second"]+"";
						panel1.BackgroundImage = Start.drawClock( 
						    Image.FromFile(Path.Combine( Start.dir_clock, table.Rows[lbx_list.SelectedIndex]["Name"]+".png" )),
							new Point(0,0), 
							Color.FromName( table.Rows[lbx_list.SelectedIndex]["Hour"]+"" ),
							Color.FromName(table.Rows[lbx_list.SelectedIndex]["Minute"]+""),
							Color.FromName(table.Rows[lbx_list.SelectedIndex]["Second"]+"")
						    );
					}catch{
						panel1.BackgroundImage = null;
					};
					break;
				case "cmb_hour":
				case "cmb_minute":
				case "cmb_second":
					try{
						panel1.BackgroundImage = Start.drawClock( 
						    Image.FromFile(Path.Combine( Start.dir_clock, table.Rows[lbx_list.SelectedIndex]["Name"]+".png" )),
							new Point(0,0), 
							Color.FromName( cmb_hour.Text ),
							Color.FromName( cmb_minute.Text ),
							Color.FromName( cmb_second.Text )				    
						    );
					}catch{
						panel1.BackgroundImage = null;
					};
					break;
					
				case "btn_ok":
					Hashtable options = sidebarGadget.Options;
					options["Name"]   = table.Rows[lbx_list.SelectedIndex]["Name"];
					options["Hour"]   = cmb_hour.Text;
					options["Minute"] = cmb_minute.Text;
					options["Second"] = cmb_second.Text;
					options["TransparentBG"] = table.Rows[lbx_list.SelectedIndex]["TransparentBG"];
					sidebarGadget.Options = options;
					
					sidebarGadget.ini_config.setValue( "Clocks", options["Name"]+"", string.Format("{0},{1},{2},{3}", options["Hour"], options["Minute"], options["Second"], options["TransparentBG"] ) );
					this.Hide();
					break;
				case "btn_cancel": 
					this.Hide();
					break;
			}
		}
		
		
	}
}
